package de.hfu.android;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import de.hfu.helper.DataAccess;
import de.hfu.helper.DataParser;
import de.hfu.helper.Helper;
import de.hfu.model.DayPlan;
import de.hfu.model.Dish;

public class SimpleItemFactory implements ItemFactory {
	Context context;
	LayoutInflater inflater;

	public SimpleItemFactory(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public View createDayItemView(DayPlan dp, View v, ViewGroup parent) {
		if (v == null) {
			v = inflater.inflate(R.layout.dayitem, parent);
		}

		Locale locale = context.getResources().getConfiguration().locale;
		Date date = dp.getDate();

		TextView tvDay = (TextView) v.findViewById(R.id.day);
		tvDay.setText(new SimpleDateFormat("EEEE").format(date));

		TextView tvDate = (TextView) v.findViewById(R.id.date);
		tvDate.setText(new SimpleDateFormat("MMMM dd yyyy", locale)
				.format(date));

		if (Helper.isEqual(date, new Date(System.currentTimeMillis()))) {
			tvDay.setText(context.getResources().getString(R.string.today));
		} else if (Helper.isEqual(date, new Date(System.currentTimeMillis()
				+ Helper.MILLIS_IN_DAY))) {
			tvDay.setText(context.getResources().getString(R.string.tomorrow));
		}
		return v;
	}

	@Override
	public View createDishView(Dish dish, View v, ViewGroup parent) {
				
		if (v == null) {
			v = inflater.inflate(R.layout.dishitem, parent);
		}

		String dishLanguage = DataAccess.getInstance(context).getDishLanguage();
		TextView dishNameView = (TextView) v.findViewById(R.id.dishName);
		dishNameView.setText(dish.getDescription(dishLanguage));

		TextView menuNameView = (TextView) v.findViewById(R.id.menuName);
		menuNameView.setText(dish.getTitle(dishLanguage));

		RatingBar ratingBar = (RatingBar) v.findViewById(R.id.overallRate);
		ratingBar.setVisibility(View.VISIBLE);

		if (dish.getRating() == 0)
			ratingBar.setVisibility(View.INVISIBLE);
		else
			ratingBar.setRating(dish.getRating());

		return v;
	}

	@Override
	public View createMoreLessView(boolean isMore, ViewGroup parent) {
		TextView v = (TextView) inflater.inflate(R.layout.viewmoreitem, null);
		String s = "";
		if (isMore)
			s = context.getString(R.string.viewmore);
		else
			s = context.getString(R.string.viewless);

		v.setText(s);
		return v;
	}

}
